<?php
/** 
 * 评价
 *
 * Date: 2018/1/9 
 * @author jackzheng
 */
include_once(GZ_PATH . '/includes/common_func.php'); 
class commentAction extends Action{


	public $user_integral= array(1=>10,2=>20,3=>30);


	public function details(){
		GZ_Api::authToken();
		$user_id = _POST('user_id',0);
		$data['rescue_id']	 = _POST('rescue_id',0);//

		
		//
		$sql="SELECT rescue_id,status,pay_status FROM " .$this->table('rescue_log').
		" WHERE id=".$data['rescue_id']." AND user_id=".$user_id;
		$res_data = $this->getRow($sql);
		$rescue_id = 0;
		if($res_data){
			$rescue_id = $res_data['rescue_id'];

			$sql="SELECT user_name,user_integral,headimg,label FROM " .$this->table('users').
			" WHERE user_id=".$rescue_id;
			$res_users = $this->getRow($sql);

			$rescue_user = array();
			//标签
			$label = array();
			if(!empty($res_users['label'])){
				$label = json_decode($res_users['label']);
			}

			$usericon = GZ_Api::get_url('/Mapi/public/images/usericon.png');
      		$user_headimg = empty($res_users['headimg'])?$usericon:GZ_Api::get_url($res_users['headimg']);

			$rescue_user['user_name'] = $res_users['user_name'];
			$rescue_user['user_integral'] =$res_users['user_integral'];
			$rescue_user['user_headimg'] =$user_headimg;
			$rescue_user['user_label'] = $label;

			$outPut['rescue_user'] = $rescue_user;
			$outPut['common_integral'] = $this->user_integral;
			GZ_Api::outPut($outPut);
			
		}else{
			GZ_Api::message("信息不存在",0);
		}

	}


	public function add_evaluate()
	{
		GZ_Api::authToken();

		$user_id = _POST('user_id',0);
		$data['rescue_id']	 = _POST('rescue_id',0);//
		$data['content']	 = _POST('content','');//
		$data['rank']		 = _POST('rank',1);//
		$data['add_time']	 = time();
		
		if(empty($data['content'])){GZ_API::message("请输入评价信息",2);}

		//检查
		$sql="SELECT rescue_id,status,pay_status FROM " .$this->table('rescue_log').
		" WHERE id=".$data['rescue_id']." AND user_id=".$user_id;
		$res_data = $this->getRow($sql);
		$rescue_id = 0;
		if($res_data){
			$rescue_id = $res_data['rescue_id'];
			if($res_data['status']<1 || $res_data['pay_status']==0){
				GZ_Api::message("信息未结束或未支付",0);
			}
		}else{
			GZ_Api::message("信息不存在",0);
		}

		//检查
		$sql="SELECT count(*) FROM " .$this->table('rescue_evaluate').
		" WHERE rescue_id=".$data['rescue_id']." LIMIT 1";
		$res_info = $this->getOne($sql);
		if($res_info>0){
			GZ_Api::message("评价信息已存在",2);
		}

		//添加评价信息
		$res = $this->autoExecute($this->table("rescue_evaluate"),$data,"INSERT");
		if($res){
			if(!empty($rescue_id)){

				$integral = $this->user_integral[$data['rank']];
				update_user_account($rescue_id,0,$integral,2,'积分奖励：救援帮助爱心积分');

				$this->update_rescue_log($data['rescue_id']);
			}
         	GZ_Api::message("评价发布成功",1);
      	}else{
        	GZ_Api::message("评价发布失败",2);
      	}
	}


	public function update_rescue_log($id)
	{
		$up_info['status'] = 3;
		$where = ' id='.$id;
		$this->autoExecute($this->table('rescue_log'), $up_info, 'UPDATA',$where);
	}

}

?>